//body必须是<body></body>，干净的

var fs = require('fs');
var path = require('path');
var bodyRegular = /<body>(.*)<\/body>/;
var scriptRegular = /<script>(.*)<\/script>/g;
var htmlTagRegular = /<(?!%)[^>]*>/g;
var nbspRegular = /&nbsp[:|;]/g;
var ejsRegular = /^<%(.*)%>/g;

var fileArr = fs.readdirSync(path.join(__dirname, 'html'));
var promiseArr = fileArr.map(function(item){
  return new Promise(function(resolve, reject){
    //获取
    console.log(path.join(__dirname, 'html', item));
    fs.readFile(path.join(__dirname, 'html', item), function(err, data){
      if(err){
        reject('read ejs file error: ' + err)
        return;
      }

      var body = bodyRegular.exec(data.toString().split('\n').join('#$%'));
      
      if(!body || !body[0]){
        console.log(item, body);
        reject('no <body>...</body> html')
        return;
      }

      body = body[0];

      if(scriptRegular.test(body)){
        body = body.replace(scriptRegular, '');
      }

      if(nbspRegular.test(body)){
        body = body.replace(nbspRegular, '');
        
      }

      body = body.split('#$%').join('\n');

      resolve(body);
    });
  })
});

Promise.all(promiseArr)
  .then(function(result){
    for(var index in result){
      var text = result[index].replace(htmlTagRegular, '');
      text = text.split('\n').filter(function(item){
        return item.trim() != '' && !ejsRegular.test(item);
      }).join('\n');

      if(!fs.existsSync(path.join(__dirname, 'text', fileArr[index].split('.')[0]+'.txt'))){
        fs.writeFile(path.join(__dirname, 'text', fileArr[index].split('.')[0]+'.txt'), text, function(err){ 
          if(err){
            console.log(err);
            return;
          }
        });
      }
    }

    console.log('end');
  }).catch(function(e){
    console.log(e);
  });
